package io.ticktick.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.lang.Assert;
import io.ticktick.common.exception.RRException;
import io.ticktick.modules.app.form.LoginForm;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.ticktick.modules.app.dao.UserDao;
import io.ticktick.modules.app.entity.UserEntity;
import io.ticktick.modules.app.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {



    @Override
    public long login(LoginForm form) {
        UserEntity userEntity = queryById(form.getId());
        if(userEntity == null){
            throw new RRException("不存在该用户");
        }

        if(!userEntity.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
            throw new RRException("账号或密码错误");
        }

        return userEntity.getId();
    }

    @Override
    public UserEntity queryById(long userId) {
        return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("id", userId));
    }
}